﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.03.22
 * 描 述：银行代发
 */
var selectedRow;
var refreshGirdData;
var userdefault = {};

var params = {};

var bootstrap = function ($, learun) {
    "use strict";
    var companyId = '';
    var departmentId = '';
    var ztId = '';
    var page = {
        init: function () {
            userdefault = learun.clientdata.get(['usersetting']);
            page.bind();
            page.initGrid();
        },
        bind: function () {
            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                }
            });
            // 公司选择
            $('#company_select').lrselect({
                type: 'tree',
                // placeholder: '请选择机构',
                height: 300,
                maxHeight: 300,
                // 是否允许搜索
                //allowSearch: true,
                select: function (item) {
                    if (item && item.value != '-1') {
                        companyId = item.value;

                        $('#zt_select').lrselectRefresh({
                            // 访问数据接口地址
                            url: config.base_server() + 'gzzts/dropdownlist?access_token=' + config.getToken().access_token,
                            // 访问数据接口参数
                            param: {companyId: companyId},
                            select: function (ztItem) {
                                if (!ztItem || ztItem.value == '-1') {
                                    ztId = "";
                                } else {
                                    ztId = ztItem.value;

                                    $('#departmentId').lrselectRefresh({
                                        // 访问数据接口地址
                                        url: config.base_server() + 'departments/tree?access_token=' + config.getToken().access_token,
                                        // 访问数据接口参数
                                        param: {companyId: item.value, parentId: '0'},
                                    });

                                    var _zt = $("#zt_select").lrselectGet();
                                    $('#item_select').lrselectRefresh({
                                        // 访问数据接口地址
                                        url: config.base_server() + 'gzitems/dropdownlist2?access_token=' + config.getToken().access_token,
                                        // 访问数据接口参数
                                        param: {companyId: companyId, ztId: _zt},
                                        select: function (item) {
                                            if (!item || item.value == '-1') {

                                            } else {
                                                var keyword = $('#txt_Keyword').val();
                                                page.search({keyword: keyword});
                                            }
                                        }
                                    });

                                }
                            }
                        }).lrselectSet(userdefault.ztId);
                    }
                }
            });
            $('#company_select').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'companys/tree?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: {parentId: '0'}
            }).lrselectSet(userdefault.companyId);
            // 帐套选择
            $('#zt_select').lrselect({
                type: 'multiple',
                //placeholder: '请选择帐套',
                // 展开最大高度
                maxHeight: 380,
                allowSearch: true
            });
            // 部门选择
            $('#departmentId').lrselect({
                type: 'multiple',
                placeholder: '部门',
                // 是否允许搜索
                allowSearch: true,
                select: function (item) {
                    if (item && item.value != '') {
                        departmentId = item.value;
                    } else {
                        departmentId = '';
                    }
                }
            });
            // 用户类型
            $('#userCategory').lrDataItemSelect({
                code: 'UserCategorys',
                type: 'multiple',
                placeholder: "员工类型",
                allowSearch: true
            });
            // 款项选择
            $('#item_select').lrselect({
                type: 'default',
                placeholder: '请选择款项',
                // 展开最大高度
                maxHeight: 380,
                allowSearch: true
            });
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });

            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });

            //Excel导出
            $("#lr_export").on('click', function () {
                params.access_token = config.getToken().access_token;
                learun.download({
                    url: config.base_server() + 'gzbankautos/exportExcel',
                    param: params,
                    method: 'GET'
                });
            });

        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'gzbankautos/list?access_token=' + config.getToken().access_token,
                headData: [
                    {label: "员工编号", name: "usercode", width: 100, align: "center"},
                    {label: "姓名", name: "username", width: 150, align: "center"},
                    {label: "银行账号", name: "bankaccount", width: 180, align: "center"},
                    {
                        label: "金额", name: "money", width: 140, align: "right", statistics: true,
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        }
                    },
                    {label: "备注", name: "description", width: 100, align: "left", hidden: true}
                ],
                isPage: true,
                rows: 200,
                reloadSelected: true,
                isTree: false,
                mainId: 'id'
            });
        },
        search: function (param) {
            param = param || {};
            param.companyId = companyId;
            param.ztId = $("#zt_select").lrselectGet();

            var departmentId = $('#departmentId').lrselectGet();
            var userCategory = $('#userCategory').lrselectGet();
            param.departmentId = departmentId;
            param.userCategory = userCategory;

            param.columns = $('#item_select').lrselectGet();

            if (!param.columns) {
                learun.alert.warning('请选择款项！');
                return false;
            }
            params = param;

            $('#gridtable').jfGridSet('reload', param);
        }
    };

    refreshGirdData = function () {
        page.search();
    };

    page.init();
}


